/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.PetOwnerDao;
import entity.Pet;
import entity.PetOwner;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import until.BaseDAO;

/**
 *
 * @author Administrator
 */
public class PetOwnerMYSQLImpl extends BaseDAO implements PetOwnerDao {

    @Override
    public List<PetOwner> getAllOwner() {
        List<PetOwner> ls = new ArrayList<>();
        try {
            Connection con = BaseDAO.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from petowner");

            while (rs.next()) {
                PetOwner petowner = new PetOwner();
                petowner.setId(rs.getInt("ID"));
                petowner.setName(rs.getString("NAME"));
                petowner.setPassword(rs.getString("PASSWORD"));
                petowner.setMoney(rs.getInt("MONEY"));
                ls.add(petowner);
            }

            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ls;
    }

    @Override
    public PetOwner slectOwner(String name, String password) {
        PetOwner petowner = null;;
        try {
            Connection con = BaseDAO.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from petowner where NAME='" + name + "' and PASSWORD='" + password + "'");

            while (rs.next()) {
                petowner = new PetOwner();
                petowner.setId(rs.getInt("ID"));
                petowner.setMoney(rs.getInt("MONEY"));

            }

            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return petowner;
    }

    @Override
    public int Update(Pet pet, PetOwner po) {
        int state = 0;
        try {
            Connection con = BaseDAO.getConnection();
            con.createStatement().execute("update petowner set MONEY=(MONEY-" + pet.getPrice() + ") where ID=" + po.getId());
            state = 1;
            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return state;
    }

    @Override
    public int UpdateSell(Pet pet, int petOwnerId) {
        int state = 0;
        try {
            Connection con = BaseDAO.getConnection();
            con.createStatement().execute("update petowner set MONEY=(MONEY+" + pet.getPrice() + ") where ID=" + petOwnerId);
            state = 1;
            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return state;
    }

}
